package com.yitao.fdfs.utils;

import com.yitao.fdfs.constant.Constant;
import org.csource.fastdfs.ClientGlobal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Properties;

/**
 * 启动配置fast参数
 */
@Component
public class FastdfsRunner implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(FastdfsRunner.class);

    @Autowired
    private FastdfsConfig fastdfsConfig;

    @Override
    public void run(String... args) throws Exception {
        logger.info("启动加载FASTDFS配置参数开始");
        Constant.fdfsProperties = new Properties();
        Constant.fdfsProperties.setProperty(ClientGlobal.PROP_KEY_CHARSET, StringUtils.isEmpty(fastdfsConfig.getCharset()) ? ClientGlobal.g_charset : fastdfsConfig.getCharset());
        Constant.fdfsProperties.setProperty(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS, StringUtils.isEmpty(fastdfsConfig.getConnect_timeout_in_seconds()) ? ClientGlobal.g_connect_timeout + "" : fastdfsConfig.getConnect_timeout_in_seconds());
        Constant.fdfsProperties.setProperty(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS, StringUtils.isEmpty(fastdfsConfig.getNetwork_timeout_in_seconds()) ? ClientGlobal.g_network_timeout + "" : fastdfsConfig.getNetwork_timeout_in_seconds());
        Constant.fdfsProperties.setProperty(ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN, StringUtils.isEmpty(fastdfsConfig.getHttp_anti_steal_token()) ? ClientGlobal.g_anti_steal_token + "" : fastdfsConfig.getHttp_anti_steal_token());
        Constant.fdfsProperties.setProperty(ClientGlobal.PROP_KEY_HTTP_SECRET_KEY, StringUtils.isEmpty(fastdfsConfig.getHttp_secret_key()) ? ClientGlobal.g_secret_key : fastdfsConfig.getHttp_secret_key());
        Constant.fdfsProperties.setProperty(ClientGlobal.PROP_KEY_HTTP_TRACKER_HTTP_PORT, StringUtils.isEmpty(fastdfsConfig.getHttp_tracker_http_port()) ? ClientGlobal.g_tracker_http_port + "" : fastdfsConfig.getHttp_tracker_http_port());
        Constant.fdfsProperties.setProperty(ClientGlobal.PROP_KEY_TRACKER_SERVERS, StringUtils.isEmpty(fastdfsConfig.getTracker_servers()) ? null : fastdfsConfig.getTracker_servers());
        logger.info("启动加载FASTDFS配置参数完成");
    }
}
